<template>
  <header>
    <div class="goback" @click="handleGoBack">
      <i class="icon iconfont icon-back"></i>
    </div>
    <div id="title-container">
      <div class="project-title" v-show="!isEditing" @click="handleClick">
        <span>{{ title }}</span>
      </div>
      <input
        type="text"
        class="project-title-input"
        ref="titleInput"
        v-if="isEditing"
        :value="title"
        @blur="handleTitleChange"
        @keyup.enter="handleTitleChange"
      />
    </div>
  </header>
</template>

<script>
export default {
  name: "page-header",
  props: {
    title: {
      type: String,
      default: "未命名",
    },
  },
  data() {
    return {
      isEditing: false,
    };
  },
  // mounted() {
  //   this.init();
  // },
  methods: {
    // init() {
    //   const oDiv = this.$refs["testDiv"];
    //   oDiv.addEventListener("click", this.handlePseudoDbClick());
    // },
    handleGoBack() {
      console.log("go back ...");
    },
    handleClick() {
      this.isEditing = true;
      this.$nextTick(() => {
        const oInput = this.$refs.titleInput;
        oInput.focus();
        oInput.select();
      });
    },
    // 伪双击修改内容
    // handlePseudoDbClick() {
    //   let prev = Date.now();
    //   let clickCount = 0;
    //   return () => {
    //     let now = Date.now();
    //     // 点击间隔小于 500ms 或 超过 1s, 则重新开始计次
    //     if (now - prev > 1000 || now - prev < 300) {
    //       clickCount = 1;
    //     } else {
    //       clickCount = 2;
    //     }
    //     prev = now;

    //     console.log(clickCount);

    //     if (clickCount === 2) {
    //       // 执行代码
    //       this.handleClick();
    //     }
    //   };
    // },
    handleTitleChange() {
      this.isEditing = false;
      const newTitle = this.$refs.titleInput.value;
      if (this.title !== newTitle) {
        // this.title = newTitle;
        // todo: 在父组件上处理 changeTitle 事件
        this.$emit("changeTitle", newTitle);
      }
    },
  },
};
</script>

<style>
header {
  width: 100%;
  height: 40px;
  padding-top: 10px;
  padding-left: 4px;
  /* background-color: rgba(105, 105, 105, 0.8); */

  display: flex;
}

.goback {
  width: 30px;
  height: 30px;
  border-radius: 2px;
  cursor: pointer;

  display: flex;
  justify-content: center;
  align-items: center;
}

.goback > .icon {
  font-size: 20px;
}

.goback:hover {
  background-color: #eee;
  /* background-color: rgba(211, 211, 211, 0.4); */
}

#title-container {
  flex-grow: 1;
  height: 30px;
  font-size: 20px;
  font-weight: normal;
}

.project-title {
  display: inline-block;
  height: 100%;
  padding: 0 10px;
  border-radius: 2px;
  cursor: pointer;
}

.project-title:hover {
  background-color: #eee;
  /* background-color: rgba(211, 211, 211, 0.4); */
}

.project-title-input {
  width: 200px;
  height: 30px;
  padding: 0 10px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  outline: none; /* 修改原有的边框样式 */
}
.project-title-input:focus,
.project-title-input:focus-visible {
  border: 1px solid #409eff;
  border-radius: 4px;
}
</style>
